Skip to content

fix(stl): allow std::variant with no default-constructible alternative - #6305

Open
henryiii wants to merge 5 commits into
pybind:masterfrom
henryiii:fix/issue-4108
Open

henryiii wants to merge 5 commits into
pybind:masterfrom
henryiii:fix/issue-4108

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Closes #4108.

variant_caster used PYBIND11_TYPE_CASTER, which declares a Type value member. If the first alternative of the variant is not default constructible, any function that takes the variant failed to compile.

With C++17 (PYBIND11_HAS_OPTIONAL), the caster now stores the value in std::optional<Type> and writes out the parts of PYBIND11_TYPE_CASTER it needs. Without C++17 (boost::variant in C++11/14), the code is not changed.

An alternative is a new macro in cast.h that takes the storage type. That is cleaner but a larger change.

Suggested changelog entry:

  • std::variant arguments now work when the first alternative is not default constructible.

@henryiii
henryiii marked this pull request as ready for review November 24, 2026 03:57
@rwgk

rwgk commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Preserve the documented custom variant_caster extension. A variant-like type used to be able to default-construct and then accept an alternative by assignment. In C++17, the new optional<V>::emplace(alternative) requires a converting constructor instead. A default-constructible wrapper around std::variant<int, double> with alternative assignment and ADL visit, and with no converting constructor, builds on the base and fails at this head with GCC 13 in C++17 mode. Please retain an assignment path for compatible custom variants and add that case to the regression coverage. The non-default-first-alternative fix itself worked in my focused tests for both std::variant and boost::variant. Storage and load, Custom casters

A custom variant can accept an alternative by assignment only, with no
converting constructor. Use emplace(alternative) only if the variant is
not default constructible.

Assisted-by: ClaudeCode:claude-fable-5-2
@rwgk

rwgk commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

The assignment-only custom variant now works again. I reran the original C++17 repro and checked value and reference loading for the supported non-default-first std::variant. The paired STL suites passed with one expected skip each.

Gate the non-default variant tests on standard variant support. The new fixtures are enabled whenever PYBIND11_HAS_OPTIONAL is defined. With optional available and standard variant absent, they instantiate the Boost fallback. Older Boost can report its default constructor as available even though instantiating it fails for this first alternative. I forced that feature combination on the actual test source and reproduced the compile error at value.emplace(). Please require PYBIND11_HAS_VARIANT for the new fixtures, since the feature and changelog cover std::variant. Storage and assignment, Test registrations

Generated by Codex. Ralf has yet to review these findings.

Older Boost reports boost::variant as default constructible when it is not.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

🤖 AI text below 🤖

Done in 5db0d80. The non-default variant fixtures now require PYBIND11_HAS_VARIANT and PYBIND11_HAS_OPTIONAL, so they do not instantiate the Boost fallback. The two macros are defined independently in common.h, and the fix in stl.h needs std::optional, so the guard keeps both. The Boost-only compile failure was not reproduced locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[QUESTION]: Example for wrapping std::variant

2 participants